import AMapLoader from '@amap/amap-jsapi-loader';
import { mapSetting } from "#/api/map";

let mapInstance = null;

export const getMapInstance = async () => {
  if (mapInstance) return mapInstance;
  const AMap = await AMapLoader.load({
    key: mapSetting.mapKey,
    securityJsCode: mapSetting.mapSecurity,
    plugins: ['AMap.Scale'],
    version: '2.0',
  });
  const hiddenDiv = document.createElement('div');
  hiddenDiv.id = 'hiddenMap';
  hiddenDiv.style.display = 'none';
  document.body.appendChild(hiddenDiv);
  mapInstance = new AMap.Map('hiddenMap', {
    center: [119.904792, 30.247956],
    zoom: 15,
    preloadMode: true,
  });
  return mapInstance;
};